// pages/m_address/m_address.js
let memberId;
const getToken = require('../../utils/get_token.js');
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    member(that);
  } else {
    let tokens = await getToken.getToken();
    member(that);
  }
}
const member = (that) => {
  let params = ['common/receiver/list', {"id":memberId }]
  getToken.getData(function (res) {
   // res.data.lists = JSON.parse(res.data.lists)
    that.setData({
      list: res.data.lists
    })
  }, ...params)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
		boolT: true,
		list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		memberId = wx.getStorageSync('memberId');
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
  },
	changeAdd:function(e){//修改事件
		var addId = e.currentTarget.dataset.id;
		var that = this;
		if (that.data.boolT) {
			that.data.boolT = false;
			wx.navigateTo({
				url: '../cg_address/cg_address?id=' + addId,
				success: function () {
					setTimeout(function () {
						that.data.boolT = true;
					}, 500)
				}
			})
		}
	},
	newAddress: function (e) {//新建地址
		var that = this;
		if (that.data.boolT) {
			that.data.boolT = false;
			wx.navigateTo({
				url: '../new_address/new_address',
				success: function () {
					setTimeout(function () {
						that.data.boolT = true;
					}, 500)
				}
			})
		}
	},
	changeDef:function(e){//改变默认地址
		var aId = e.currentTarget.dataset.id;
		var that = this;
		wx.showModal({
			title: '提示',
			content: '是否设置为默认地址？',
			success: function (res) {
				if (res.confirm) {
          let params = ['common/receiver/settingDefault', {"memberId": memberId,"id":aId }]
          getToken.getData(function (res) {
            console.log(res.data);
            res.data.flag = JSON.parse(res.data.flag);
            if (res.data.flag) {
              member(that);
            }
          }, ...params)					
				} 
			}
		})	
	},
  changeM:function(e){//选择地址
    var aId = e.currentTarget.dataset.id;
    var that = this;
    let params = ['common/receiver/settingDefault', {"memberId": memberId,"id":aId}]
    getToken.getData(function (res) {
      res.data.flag = JSON.parse(res.data.flag);
      if (res.data.flag) {
        member(that);
        // wx.reLaunch({
        //   url: '../personal/personal'
        // })
      }
    }, ...params)		
  },
	delAdd:function(e){//删除
		var aId = e.currentTarget.dataset.id;
		var that = this;
		wx.showModal({
			title: '提示',
			content: '是否删除当前地址？',
			success: function (res) {
				if (res.confirm) {
          let params = [ 'common/receiver/delete', {"id": aId}]
          getToken.getData(function (res) {
            console.log(res.data);
            res.data.flag = JSON.parse(res.data.flag);
            if (res.data.flag) {
              member(that);
            }
          }, ...params)		
				} 
			}
		})
	},
	onShow: function () {
		var that = this;
    ckToken(that);
	}  
})